﻿Public Class VGridExt
    Private WithEvents _aMetaDataTable As CommonExt.MetaDataTable
    Private _aQueryName As String


    Public Property QueryName As String
        Get
            QueryName = _aQueryName
        End Get
        Set(ByVal value As String)
            _aQueryName = value
        End Set
    End Property


    Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
    End Sub


    Public Sub New(ByVal aQueryName As String)
        ' This call is required by the designer.
        InitializeComponent()
        _aQueryName = aQueryName
        ' Add any initialization after the InitializeComponent() call.
        initVGridExt()
    End Sub

    Public Sub initVGridExt()
        initData()
        VGridControl1.DataSource = _aMetaDataTable
        VGridControl1.RetrieveFields()
        initDisplay()
    End Sub
    Public Sub initData()
        'Get the  root data
        _aMetaDataTable = New CommonExt.MetaDataTable() With {.QueryName = _aQueryName}
        _aMetaDataTable.FillBusinessApplicationData()
    End Sub

    Private Sub initDisplay()
     
    End Sub

    Public Sub CUD()
        Try
            ' extTable.Adapter.Update(extTable)
        Catch sqlex As System.Data.SqlClient.SqlException
            'HACK: On Utilise les codes de SQL. Attention si les codes change entre ADO et/ou SQL
            Select Case sqlex.Number
                Case 2601, 2627  'Unique Index/Constriant Violation
                    MessageWindow.ShowMessage(String.Format("Record Already Exist, Operation Aborded: {0}", sqlex.Message), MsgBoxStyle.Information)
                    'On Rafraichie
                Case Else
                    MessageWindow.ShowMessage(String.Format("Database Error: {0} #{1} ", sqlex.Message, sqlex.Number), MsgBoxStyle.Information)
            End Select
        Catch dbcex As System.Data.DBConcurrencyException
            MessageWindow.ShowMessage("Concurrency Issue, Record Already Modified", MsgBoxStyle.Information)
        Catch ex As Exception
            MessageWindow.ShowMessage(String.Format("Row Cannot Be Save: {0}. Make Sure You Have a CUD Command", ex.Message), MsgBoxStyle.Information)
        End Try

        '_aDataset.AcceptChanges()
        'outstandingIssue : les lignes cidessous sont une patch...
        'ca plante dans metadatable..
        '        System.InvalidCastException was unhandled
        '  Message=Specified cast is not valid.
        '        Source = ActiveFramework.CommonExt
        'StackTrace:
        '       at SCIEF.ActiveFramework.CommonExt.MetaDataTable.RowUpdated(Object sender, SqlRowUpdatedEventArgs e) in D:\VB\SCIEF\VS2010\DotNet3\ActiveFramework\ActiveFramework.CommonExt\Common\MetaData\MetaDataTable.vb:line 402
        '       at System.Data.SqlClient.SqlDataAdapter.OnRowUpdated(RowUpdatedEventArgs value)
        '       at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows, DataTableMapping tableMapping)
        '       at System.Data.Common.DbDataAdapter.UpdateFromDataTable(DataTable dataTable, DataTableMapping tableMapping)
        '       at System.Data.Common.DbDataAdapter.Update(DataTable dataTable)
        initData()
        VGridControl1.RefreshDataSource()
    End Sub
End Class




